/**
 * 描述: 封装网络请求 axios
 * 作者: kvmin
 * 时间: 2017年09月14日 11:09
 */
// 必须先导入 /static/common/config.js

var BASE_URL = 'http://127.0.0.1:8081/HS_Admin';
var API_BASE = BASE_URL + '/api/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.common['Authorization'] = "270837b946b34495a0baf54018999305";
var layerLoading;
var HttpUtils = {
    //基础选项
    options: {
        url: '', //请求地址(必填)
        showLoading: true, //是否显示加载提示
        showCallBackTip: false, //是否显示返回的提示
        data: {},
        success: function (response) {
        },
        error: function (error) {
        },
        before: function (info) {
        },
        end: function () {
        }
    },
    loadIndex: 0,
    showOnLoad: function () {
        HttpUtils.loadIndex = top.layer.load();
        // setTimeout(function(){
        // 		top.layer.close(HttpUtils.loadIndex);
        // }, 10000);
    },
    hiddenLoad: function () {
        top.layer.close(HttpUtils.loadIndex);
    },
    POST: function (option) {
        option = HttpUtils.init(option);
        axios.post(option.url, HttpUtils.formParams(option.data)).then(function (response) {
            option.success(response);
        }).catch(function (error) {
            option.error(error);
        })
    },
    GET: function (option) {
    	if(!option.data) option.data={};
    	option.data['cachet'] = new Date().getTime();
        option = HttpUtils.init(option);
        axios.get(option.url, {
        	headers: {
        		'Content-Type': 'application/x-www-form-urlencoded'
        	}, params: option.data})
            .then(function (response) {
                option.success(response);
            })
            .catch(function (error) {
                option.error(error);
            })
    },
    PUT: function (option) {
        option = HttpUtils.init(option);
        axios.put(option.url, HttpUtils.formParams(option.data))
            .then(function (response) {
                option.success(response);
            }).catch(function (error) {
            option.error(error);
        })
    },
    DELETE: function (option) {
        option = HttpUtils.init(option);
        axios.delete(option.url, {params:option.data})
            .then(function (response) {
                option.success(response);
            }).catch(function (error) {
            option.error(error);
        })
    },
    formParams: function (data) {
        var formBody = [];
        for (var property in data) {
            //encodeURIComponent: 解决（ URLDecoder: Illegal hex characters in escape (%) pattern ）
             var encodedKey = encodeURIComponent(property);
             var encodedValue = encodeURIComponent(data[property]);
            if(isNotBlank(data[property])){
                formBody.push(encodedKey + "=" + encodedValue);
            }
            // if(isNotBlank(data[property])){
            //     formBody.push(property + "=" + data[property]);
            // }
        }
        return formBody.join("&");//解决后台 request get 问题
    },
    init: function (options) {
        return HttpUtils.options = $.extend({
            url: '',
            showLoading: true,
            showCallBackTip: false,
            data: {},
            success: function (response) {
            }, error: function (error) {
            },
            before: function (info) {
            }, end: function () {
            }
        }, options)
    }
}

function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
    }
    return "";
}

